// pages/weather/weather.js
Page({

  /**
   * 页面的初始数据
   */
  data: {
    weather_data: null,

    weather_desc: '天气数据获取中...',

  },

  // https://api.seniverse.com/v3/weather/daily.json?key=SG6tbxo7FcwyC1Mof&location=linyi&unit=c&start=0&days=2

  /**
   * 生命周期函数--监听页面加载
   */
  onLoad: function (options) {

  },

  /**
   * 生命周期函数--监听页面初次渲染完成
   */
  onReady: function () {

    let that = this;

    wx.request({
      url: 'https://api.seniverse.com/v3/weather/daily.json?key=SG6tbxo7FcwyC1Mof&location=linyi&unit=c&start=0&days=2',

      data: {
        param1: '',
        param2: ''
      },

      success: function (res) {
        console.log('天气数据获取成功...');
        console.log(res.data);

        var city = res.data.results[0].location.name;
        var daily = res.data.results[0].daily[0];
        var txt_day = daily.text_day; // 白天天气
        var txt_night = daily.text_night; // 夜间天气
        var high = daily.high; // 最高气温
        var low = daily.low; // 最低气温
        var wind_direction = daily.wind_direction; // 风向
        var wind_scale = daily.wind_scale; // 风力

        var desc = city + '   白天' + txt_day + ' 夜间' + txt_night + '   ' + low + '~' + high + '℃   ' + wind_direction + '风' + wind_scale + '级';

        that.setData({
          weather_desc: desc
        });
      },

      fail: function () {
        console.log('请求失败...');
        that.setData({
          weather_desc: '天气数据获取失败...'
        });
      },

      complete: function () {
        console.log('请求完成...');
      }

    })
  },

  /**
   * 生命周期函数--监听页面显示
   */
  onShow: function () {

  },

  /**
   * 生命周期函数--监听页面隐藏
   */
  onHide: function () {

  },

  /**
   * 生命周期函数--监听页面卸载
   */
  onUnload: function () {

  },

  /**
   * 页面相关事件处理函数--监听用户下拉动作
   */
  onPullDownRefresh: function () {

  },

  /**
   * 页面上拉触底事件的处理函数
   */
  onReachBottom: function () {

  },

  /**
   * 用户点击右上角分享
   */
  onShareAppMessage: function () {

  }
})